/*
 *
 */
#include "common.h"
#include "module.h"
#include "text.h"
#include "widgets.h"
#include "statusbar.h"
#include "module_opened.h"
#include "popup.h"
#include "handlers_module_opened.h"

#include "selector.h"

/******* 先简单地只管理一个文件，没有 NoteBook 页 *********/
void
cb_save_file(void)
{
	// save current file, if modified
	if ( ! IsMainModuleModified() )
	{
	    // New file
	    if ( NULL == GetModuleTextMain() )
	    {
	        if (IsMainTextViewHaveContents())
	        {
	            SaveFileAs(NULL);

/*	            gint result = 
	                RunDialogMessageQuestion(pWidgetMain,GTK_MESSAGE_QUESTION,
	                                 _("Save changes to file?"));

	            switch (result )
	            {
	                case GTK_RESPONSE_YES:
                         SaveFileAs(NULL);
                         break;
	                case GTK_RESPONSE_NO:
                         return;
                         break;
	                default:
	                     return;
	                     break;

                }
 */ 
	        }
	    }
	    else
		    PrintStatusbar(_("*** None Modified, None save."));
		return;
	}
	else
	{
		if ( ! SaveActiveFile() )
		{
			PrintStatusbar(_("*** Save File Fail."));
			return ;
		}
		SetMainModuleModified(FALSE);
		// reload ActiveModule
		module_t *pActiveModule = GetModuleTextMain();//ActiveModule();
		if ( pActiveModule )
		{
			FreeModuleBuffer(pActiveModule->pModuleName);
			ModuleBuffer(pActiveModule->pModuleName, &pActiveModule->moduleSize);
		}
		else
		{
			PrintStatusbar(_("*** Cannot RELOAD file."));
		}
	}
	return;
}

void
cb_save_file_as(void)
{
	// save current file to another file

    module_t * pModule = GetModuleTextMain();
    SaveFileAs(pModule);    // pModule == NULL also OK!
}

void
cb_save_all_file(void)
{
    return cb_save_file();
}

void
cb_open_file(void)
{
	// open a file
	FileInfo  fi1;
	fi1.filename=NULL;
	fi1.charset=NULL;
	fi1.charset_flag=FALSE;
	fi1.lineend=CHAR_LF;
	
	FileInfo *fi;
	fi = get_fileinfo_from_selector(&fi1, OPEN);
	if (fi)
	    g_print("Open file:%s\t%s\t%c\n",fi->filename,fi->charset,fi->lineend);
}

void
cb_new_file(void)
{
	// create new file
	// save current file, if modified
	if ( ! IsMainModuleModified() )
	{
	    // New file
	    if ( NULL == GetModuleTextMain() )
	    {
	        if (IsMainTextViewHaveContents())
	        {
                gint result = 
	                RunDialogMessageQuestion(pWidgetMain,
	                                 _("Save changes to file?"));

	            switch (result )
	            {
	                case GTK_RESPONSE_YES:
	                    // save file fail
                         if (SaveFileAs(NULL) == FALSE )
                            return;
                         break;
	                case GTK_RESPONSE_NO:
                         ;
                         break;
	                default:
	                     return;
	                     break;
                }
	        }
	    }
	    else
		    PrintStatusbar(_("*** None Modified, None save."));
	}
	else
	{ // modified , save it!
	    gint result = 
	           RunDialogMessageQuestion(pWidgetMain,
	                                 _("Save changes to file?"));
	    
    	switch(result)
	    {
	    
	    case GTK_RESPONSE_OK:
	    	    
	    	if ( ! SaveActiveFile() )
	    	{
	    		PrintStatusbar(_("*** Save File Fail."));
	    	}
	    	SetMainModuleModified(FALSE);
	    	// reload ActiveModule
	    	module_t *pActiveModule = GetModuleTextMain();//ActiveModule();
	    	if ( pActiveModule )
	    	{
	    		FreeModuleBuffer(pActiveModule->pModuleName);
	    		ModuleBuffer(pActiveModule->pModuleName, &pActiveModule->moduleSize);
	    	}
	    	else
	    	{
	    		PrintStatusbar(_("*** Cannot RELOAD file."));
	    	}
		
		    break;
		    
		 case GTK_RESPONSE_NO:
		    /* do nothing */
		    break;
		    
		 default:
		    return;
		    break;
		}
	}
    ///
    SetActiveTextWidget(pWidgetTextMain);
    GiveFocusToTextWidget(pWidgetTextMain);
    
    ClearTextWidget();

    RecordActiveModule(NULL);
    RecordActiveModuleName("\0");
    
    GtkTextBuffer *pTextBuf = gtk_text_view_get_buffer(
                        GTK_TEXT_VIEW(pWidgetTextMain));
    gtk_text_buffer_set_modified(pTextBuf, FALSE);
}

/*
 * set hypersrc editable or uneditable
 */
void
cb_editable(void)
{
	static gboolean editable=TRUE;
	
	static gboolean busy = FALSE;
	
	if ( busy )
	    return;
	busy = TRUE;
	
	if ( NULL == pWidgetTextMain )
	    return;

	if ( editable )
	{
	    PrintStatusbar( _("Uneditable") );
	    editable = FALSE;
	    // change toolbar button's pixmap
	}
	else
	{
	    PrintStatusbar( _("Editable") );
	    editable = TRUE;
	}

    gtk_text_view_set_editable( GTK_TEXT_VIEW( pWidgetTextMain ) ,
	                            editable );

    busy = FALSE;
    
    return;
}

void
cb_refresh_proj(void)
{
    g_print("Rebuild tags\n");
}

void
cb_full_screen(void)
{
    g_print("Full screen\n");
}

void
cb_hide_tags(void)
{
    g_print("hide/show tags\n");
}

void
cb_hide_left_bottom(void)
{
    g_print("hide/show left and bottom\n");
}

/****************************************************************************
 * Callback for TextViewBuffer 'modified-changed'
 ****************************************************************************/
void
cb_modified_changed(GtkTextBuffer *buffer,
					GtkWidget     *view)
{
	static gssize count=0;
	DebugMsg("Modified-Changed %d times.\n", ++count);

//	if ( gtk_text_buffer_get_modified(GTK_TEXT_BUFFER(buffer)))
	module_t *pModule = GetModuleTextMain();//ActiveModule();
	if ( IsMainModuleModified() )
	{
		DebugMsg("Modified.");	

		if ( pModule )
			pModule->modified = TRUE;
	}
	else
	{
		DebugMsg("UnModified.");
		if ( pModule )
			pModule->modified = FALSE;
	}

	SetMainWindowTitle();
	return;
}

/****************************************************************************
 * Callback for TextBuffer's `changed'
 ****************************************************************************/
void
cb_changed( GtkTextBuffer *buffer,
			GtkWidget     *view)
{
#ifdef HYPERSRC_DEBUG
	static gssize count=0;
	g_print("Changed %d times.\n", ++count);
#endif
    return;
}

